import java.util.concurrent.*;

/**
 * @projectName: java-daily-questions
 * @package: PACKAGE_NAME
 * @className: ThreadTest
 * @author: ZM
 * @description:
 * @date: 2021/8/19 11:26
 * @version: 1.0
 */
public class ThreadTest {

    static class MyThread extends Thread {
        @Override
        public void run() {
            System.out.println("extends Thread class!");
            System.out.printf("当前线程name：%s, 当前线程id：%s\n", Thread.currentThread().getName(), Thread.currentThread().getId());
        }
    }

    static class MyRunnable implements Runnable {

        @Override
        public void run() {
            System.out.println("implements Runnable interface!");
            System.out.printf("当前线程name：%s, 当前线程id：%s\n", Thread.currentThread().getName(), Thread.currentThread().getId());
        }
    }

    static class MyCallAble implements Callable<String> {

        @Override
        public String call() throws Exception {
            System.out.printf("当前线程name：%s, 当前线程id：%s\n", Thread.currentThread().getName(), Thread.currentThread().getId());
            return "implements Runnable interface!";
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1.继承Thread类
        MyThread t1 = new MyThread();
        //2.实现Runnable接口
        MyRunnable runnable = new MyRunnable();
        Thread t2 = new Thread(runnable);
        //3.实现callable接口
        MyCallAble callAble = new MyCallAble();
        FutureTask<String> futureTask = new FutureTask<>(callAble);
        Thread t3 = new Thread(futureTask);
        t1.start();
        t2.start();
        t3.start();
        System.out.println("futureTask.get() = " + futureTask.get());
        //4.线程池获取线程
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.submit(() -> {
            System.out.println("Executors create a singleThread");
        });
    }
}
